package com.mrduan.otgfilemanager.web;

import com.github.mjdev.libaums.fs.UsbFile;
import com.github.mjdev.libaums.fs.UsbFileInputStream;
import com.mrduan.otgfilemanager.ui.getstrong.GetStrongActivity;
import com.orhanobut.logger.Logger;

import java.io.IOException;

import fi.iki.elonen.NanoHTTPD;

/**
 * Created by duany on 2018/5/8.
 */

public class OTGNanoHTTPD extends NanoHTTPD {

    public OTGNanoHTTPD() throws IOException {
        super(8888);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);

    }

//    public OTGNanoHTTPD(String hostname, int port) {
//        super(hostname, port);
//    }


    @Override
    public Response serve(IHTTPSession session) {

//        Response response = newChunkedResponse(Response.Status.OK,"application/octet-stream",mp4Input);
//        response.addHeader("Accept-Ranges", "bytes");
//        response.addHeader("Content-Disposition", "attachment; filename="+"test.jpg");//可以这里返回文件名称

        Logger.d("进入！");

        return getResponse(session.getUri());

    }

    public Response getResponse(String uri) {
//        String[] fNames = uri.split("/");
        if (GetStrongActivity.root != null) {

            String[] fNames = uri.split("/");

            UsbFile file = GetStrongActivity.root;

            for (int i = 1; i < fNames.length; i++) {
                String fName = fNames[i];
                if (fName.length() == 0 || fName.equals("")) {
                    continue;
                }
                if (file != null) {
                    file = getFile(file, fName);
                }
            }

            if (file == null) {
                return null;
            }

            Logger.d("名称：" + file.getName());

            Response response = null;

            if (file.getName().indexOf("mp4") != -1 || file.getName().indexOf("MP4") != -1) {
                response = newChunkedResponse(Response.Status.OK, "video/mp4", new UsbFileInputStream(file));
//                response.addHeader("Accept-Ranges", "bytes");
//                response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());//可以这里返回文件名称
            } else {
                response = newChunkedResponse(Response.Status.OK, "application/octet-stream", new UsbFileInputStream(file));
                response.addHeader("Accept-Ranges", "bytes");
                response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());//可以这里返回文件名称

            }

            return response;
        } else {
            return null;
        }

    }

    public UsbFile getFile(UsbFile parent, String fName) {

        if (parent.isDirectory()) {
            try {
                UsbFile[] files = parent.listFiles();
                for (UsbFile arg : files) {
                    if (arg.getName().equals(fName)) {
                        Logger.d("名称：" + arg.getName());
                        return arg;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

}
